/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  FilePathChecker.java                                      
 *  File Contents Outline :  文件路径检查                  
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */


package cn.edu.thu.log.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

public class FilePathChecker {
	public static boolean isExist(String hdfspath) {
		boolean result = false;
		String dst = hdfspath;
		
		try {
			FilePathChecker.getDirectoryFromHdfs();
			Configuration conf = new Configuration();
			FileSystem fs = FileSystem.get(URI.create(dst), conf);
			if(fs.exists(new Path(dst))){
				result = true;
			} else{
				result = false;
			}
		} catch (Exception e) {

		} finally {
		}
		return result;
	}

	private static void getDirectoryFromHdfs() throws FileNotFoundException,
			IOException {
		try {
			String dst = "hdfs://10.10.192.18:9000/warehouse/";
			Configuration conf = new Configuration();
			FileSystem fs = FileSystem.get(URI.create(dst), conf);

			if (fs.isDirectory(new Path(dst))) {
				FileStatus fileList[] = fs.listStatus(new Path(dst));
				int size = fileList.length;
				System.out.println("hello");
				for (int i = 0; i < size; i++) {
					System.out.println("name:"
							+ fileList[i].getPath().getName() + "/t/tsize:"
							+ fileList[i].getLen());
				}
				fs.close();
			} else {
				fs.copyToLocalFile(new Path(dst), new Path("E://good/"));
				/*
				 * FSDataInputStream fds = fs.open(new Path(dst)); String str;
				 * while ((str = fds.readLine())!= null) {
				 * System.out.println(str); }
				 */
			}
		} catch (Exception e) {

		}
	}

}
